+function($) {
	'use strict';

	var cache = {};
	
	var Check = function(element, options) {
		var that = this;
		that.element = $(element);
		Check.OPTIONS = $.extend({}, Check.OPTIONS, options);
		this.init(that.element);
	}
	
	Check.OPTIONS = {className : "btn-info", cookieName : "check", cookieOptions: {expires : 7, path : '/'}, pkey: 'p_', ckey: 'c_'}
	
	Check.prototype.init = function(element){
		var settings = Check.OPTIONS;
		var cookieStr = $.cookie(settings.cookieName);
		var cacheObj = {};
		if(typeof cookieStr != 'undefined'){
			cacheObj = $.parseJSON(cookieStr);
			for(var i in cacheObj){
				var index = i.replace(settings.pkey, ''), formGroupA = $(element).find('.form-group:eq(' + index + ') a');
				for(var _i in cacheObj[i]){
					var cindex = _i.replace(settings.ckey, '');
					if(cacheObj[i][_i] == 1) {
						formGroupA.eq(cindex).addClass(settings.className);
					}else{
						formGroupA.eq(cindex).removeClass(settings.className);
					}
				}
			}
			this.detected(element);
		}else{
			this.initCookie(element);
		}
	}
	
	Check.prototype.detected = function(element){
		var checkObj = this;
		element.find('.form-group').each(function(index, item){
			var multi = (typeof $(item).data('multi')) == 'undefined' ? 0 : $(item).data('multi');
			var ccache = {};
			cache['index_' + index] = {};
			$(item).find('a').on('click', function(){
				if(multi){
					if($(this).index() === 0){
						$(this).addClass(Check.OPTIONS.className).siblings().removeClass(Check.OPTIONS.className);
					}else{
						$(this).siblings(':eq(0)').removeClass(Check.OPTIONS.className);
						if($(this).hasClass(Check.OPTIONS.className)){
							$(this).removeClass(Check.OPTIONS.className);
						}else{
							$(this).addClass(Check.OPTIONS.className);
						}
					}
				}else{
					$(this).addClass(Check.OPTIONS.className).siblings().removeClass(Check.OPTIONS.className);
				}
				checkObj.setCookie(element, $(item).index(), $(this).index());
			});
		});
	}
	
	Check.prototype.initCookie = function(element){
		element.find('.form-group').each(function(index, item){
			var pindex = index;
			var ccache = {};
			cache[Check.OPTIONS.pkey + index] = {};
			$(item).find('a').each(function(cindex, citem){
				ccache[Check.OPTIONS.ckey + cindex] = Number($(citem).hasClass(Check.OPTIONS.className))
			});
			cache[Check.OPTIONS.pkey + pindex] = ccache;
		});
		$.cookie(Check.OPTIONS.cookieName, JSON.stringify(cache), Check.OPTIONS.cookieOptions);
	}
	
	Check.prototype.setCookie = function(element, pindex, cindex) {
		var cache = $.parseJSON($.cookie(Check.OPTIONS.cookieName));
		var pObj = element.find('.form-group:eq(' + pindex + ')');
		var multi = pObj.data('multi');
		for(var i in cache){
			if(i.replace(Check.OPTIONS.pkey, '') == pindex){
				if(multi) cache[i]['c_0'] = 0;
				for(var _i in cache[i]){
					var _cindex = _i.replace(Check.OPTIONS.ckey, '');
					if(!multi){
						cache[i][_i] = (_cindex == cindex) ? 1 : 0;
					}else{
						var val = Number(pObj.find('a:eq(' + cindex + ')').hasClass(Check.OPTIONS.className));
						if(cindex == 0){
							cache[i][_i] = (_cindex == cindex) ? 1 : 0;
						}else{
							if(_cindex == cindex){
								cache[i][_i] = val;
								break;
							}
						}
					}
				}
			}
		}
		$.cookie(Check.OPTIONS.cookieName, JSON.stringify(cache), Check.OPTIONS.cookieOptions);
	}

	$.fn.check = function (options) {
		var that = this;
		return that.each(function() {
			new Check(this, options);
		})
	}
}(jQuery);
